package mytestjasperreportproject

import java.text.SimpleDateFormat

import export.ExportType;
import export.ExportHandlerImpl

class TestExportController {
	TestExportService testExportService
	
    def export() {
		try {
			// Build file name
			String timeInstance = new SimpleDateFormat("ddMMyyyyHHmmss").format(Calendar.getInstance().getTime())
			String fileName = "testExport.xls"

			// Set base path to store the exported file
			String basePath = "C:\\"

			Map exportParams = [
						"requestParams" : ['id' : 'testid','name' : 'testname'],
						"fileName" : fileName,
						"basePath" : basePath,
						"exportType" : ExportType.XLS
					];
			this.doExport(exportParams);

			// return exported file to client
			fileName = basePath + fileName
			def exportHandlers= new ExportHandlerImpl()
			exportHandlers.sendFile(response, fileName)
		} catch(Exception e) {
			log.error("An error occurred when exporting file: ", e)
		}
	}
	
	protected def doExport(Map exportParams) {
		List data = [['id' : 'testid1','name' : 'testname1'],
					 ['id' : 'testid2','name' : 'testname2'],
					 ['id' : 'testid3','name' : 'testname3'],
					 ['id' : 'testid4','name' : 'testname4'],
					 ['id' : 'testid5','name' : 'testname5'],
					 ['id' : 'testid6','name' : 'testname6'],
					 ['id' : 'testid7','name' : 'testname7']]
		
		testExportService.export(data, exportParams['fileName'],
                                        exportParams['basePath'], exportParams['exportType'], null)
	}
}
